package codec

import (
	"gitee.com/yanwc/ebicycle/device/utils"

	"github.com/panjf2000/gnet"
)

type BikeFrameCodec struct{}

var _ gnet.ICodec = (*BikeFrameCodec)(nil)

func (cc *BikeFrameCodec) Decode(c gnet.Conn) (b []byte, err error) {
	buf := c.Read()
	idx, err := Tag.IndexOf(buf)
	if err != nil {
		return nil, err
	}

	b = append(b, buf[:idx]...)
	c.ResetBuffer()
	return
}

func (cc *BikeFrameCodec) Encode(c gnet.Conn, buf []byte) (out []byte, err error) {
	out = append(out, utils.NumberToBytesWithBigEndian(uint16(Tag))...)
	out = append(out, buf...)
	return
}
